using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class AppMsgDisplayer : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "AppMsgDisplayer";
		private const double cDPicH = 108.0;
		private const double cDPicW = 108.0;
		private const double cDPicMarg = 15.0;
		private const double cDCtrlW = 372.0;
		private const double cDAppend = 5.0;
		private const string cStrHttpHerader = "http://";
		private const int cNMaxCacheCnt = 2;
		private bool mBIsFirstLoaded = true;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(ContentObject), typeof(AppMsgDisplayer), new PropertyMetadata(null, new PropertyChangedCallback(AppMsgDisplayer.OnItemObjectPropertyChanged)));
		private EventWatcher mWatchxxx;
		private object mObjectWatcher = new object();
		private string mStrArticleUrl = "";
		private static List<AppMsgDisplayer> sTimelineItem = new List<AppMsgDisplayer>();
		internal StackPanel LayoutRoot;
		internal Grid picContainer;
		internal TextBlock txtTitle;
		internal TextBlock txtLink;
		private bool _contentLoaded;
		public event AppMsgDisplayer.ListItemClick ItemClick;
		public ContentObject ItemObject
		{
			get
			{
				return (ContentObject)base.GetValue(AppMsgDisplayer.ItemObjectProperty);
			}
			set
			{
				base.SetValue(AppMsgDisplayer.ItemObjectProperty, value);
			}
		}
		public AppMsgDisplayer()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			ShowObjectInMemory.AddOneObjectRecord(this);
		}
		~AppMsgDisplayer()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == AppMsgDisplayer.ItemObjectProperty)
			{
				(dependencyObject_0 as AppMsgDisplayer).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		public void OnVisible()
		{
			using (IEnumerator<UIElement> enumerator = this.picContainer.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					if (current is CirclePagePicItem)
					{
						(current as CirclePagePicItem).ShowImg();
					}
				}
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
		}
		private void ClickEffectGrid_mClicked(object sender, object args)
		{
			this.gotoLink(this.mStrArticleUrl);
		}
		private void picctrl_ItemClick(object sender, object object_0)
		{
			this.gotoLink(this.mStrArticleUrl);
		}
		private void ClickEffectGrid_mClicked_1(object sender, object args)
		{
		}
		private void gotoLink(string string_0)
		{
			ToolFunction.GotoWebLinkInWebView(string_0, GetA8KeyScene.MMGETA8KEY_SCENE_TIMELINE);
		}
		private void LayoutRoot_Click(object sender, RoutedEventArgs e)
		{
			this.gotoLink(this.mStrArticleUrl);
		}
		private void RefreshItem()
		{
			if (this.ItemObject == null)
			{
				this.txtTitle.set_Text("");
				this.setLink("");
				this.showTexts();
				this.picContainer.set_Visibility(1);
				return;
			}
			this.mStrArticleUrl = this.ItemObject.strContentUrl;
			if (this.ItemObject.strDescription != null && this.ItemObject.strDescription.get_Length() > 70)
			{
				this.setLink(this.ItemObject.strDescription.Substring(0, 70) + "...");
			}
			else
			{
				this.setLink(this.ItemObject.strDescription);
			}
			if (this.ItemObject.strTitle != null && this.ItemObject.strTitle.get_Length() > 30)
			{
				this.txtTitle.set_Text(this.ItemObject.strTitle.Substring(0, 30) + "...");
			}
			else
			{
				this.txtTitle.set_Text(this.ItemObject.strTitle);
			}
			List<TloMedia> mediaList = this.ItemObject.mediaList;
			if (mediaList != null && mediaList.get_Count() != 0)
			{
				this.txtTitle.set_MaxWidth(234.0);
				this.txtLink.set_MaxWidth(this.txtTitle.get_MaxWidth());
				this.picContainer.set_Visibility(0);
				if (this.picContainer.get_Children().get_Count() == 0)
				{
					CirclePagePicItem oneItem = CirclePagePicItem.GetOneItem();
					oneItem.mBIsDelayLoadImage = true;
					this.picContainer.get_Children().Add(oneItem);
					oneItem.ItemClick += new CirclePagePicItem.ListItemClick(this.picctrl_ItemClick);
				}
				CirclePagePicItem circlePagePicItem = this.picContainer.get_Children().get_Item(0) as CirclePagePicItem;
				if (circlePagePicItem != null)
				{
					circlePagePicItem.set_Height(108.0);
					circlePagePicItem.set_Width(108.0);
					circlePagePicItem.ObjMediaInfo = mediaList.get_Item(0);
					if (this.ItemObject.nStyle == 4)
					{
						this.txtTitle.set_Text(mediaList.get_Item(0).strTitle);
						this.setLink(mediaList.get_Item(0).strDescription);
					}
				}
				this.showTexts();
				return;
			}
			this.picContainer.set_Visibility(1);
			this.txtTitle.set_MaxWidth(352.0);
			this.txtLink.set_MaxWidth(this.txtTitle.get_MaxWidth());
			this.showTexts();
		}
		private void showTexts()
		{
			if (string.IsNullOrEmpty(this.txtTitle.get_Text()))
			{
				this.txtTitle.set_Visibility(1);
			}
			else
			{
				this.txtTitle.set_Visibility(0);
			}
			if (string.IsNullOrEmpty(this.txtLink.get_Text()))
			{
				this.txtLink.set_Visibility(1);
				return;
			}
			this.txtLink.set_Visibility(0);
		}
		private void setLink(string strLink)
		{
			if (strLink == null)
			{
				this.txtLink.set_Text("");
				return;
			}
			if (strLink.StartsWith("http://"))
			{
				strLink = strLink.Remove(0, "http://".get_Length());
				int num = strLink.IndexOf('/');
				if (num != -1)
				{
					strLink = strLink.Remove(num);
				}
			}
			this.txtLink.set_Text(strLink);
		}
		public static AppMsgDisplayer GetOneItem()
		{
			if (AppMsgDisplayer.sTimelineItem != null && AppMsgDisplayer.sTimelineItem.get_Count() != 0)
			{
				AppMsgDisplayer result = Enumerable.First<AppMsgDisplayer>(AppMsgDisplayer.sTimelineItem);
				AppMsgDisplayer.sTimelineItem.RemoveAt(0);
				return result;
			}
			return new AppMsgDisplayer();
		}
		public static int GetCacheCnt()
		{
			if (AppMsgDisplayer.sTimelineItem == null)
			{
				return 0;
			}
			return AppMsgDisplayer.sTimelineItem.get_Count();
		}
		public static void PushOneItem(AppMsgDisplayer item)
		{
			if (AppMsgDisplayer.sTimelineItem == null)
			{
				AppMsgDisplayer.sTimelineItem = new List<AppMsgDisplayer>();
			}
			item.ClearMemory();
			if (AppMsgDisplayer.sTimelineItem.get_Count() > 2)
			{
				return;
			}
			AppMsgDisplayer.sTimelineItem.Add(item);
		}
		public void ClearMemory()
		{
			this.ItemObject = null;
			if (this.picContainer.get_Children().get_Count() != 0)
			{
				CirclePagePicItem circlePagePicItem = this.picContainer.get_Children().get_Item(0) as CirclePagePicItem;
				circlePagePicItem.ItemClick -= new CirclePagePicItem.ListItemClick(this.picctrl_ItemClick);
				if (circlePagePicItem != null)
				{
					CirclePagePicItem.PushOneItem(circlePagePicItem);
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Circle/UI/AppMsgDisplayer.xaml", 2));
			this.LayoutRoot = (StackPanel)base.FindName("LayoutRoot");
			this.picContainer = (Grid)base.FindName("picContainer");
			this.txtTitle = (TextBlock)base.FindName("txtTitle");
			this.txtLink = (TextBlock)base.FindName("txtLink");
		}
	}
}
